﻿using D.Wx.Entities;
using D.Wx.NLogsUtil;
using Hangfire;


namespace D.Wx.Jobs
{
    /// <summary>
    /// 定时清理用户信息
    /// </summary>
    public class AutoCleanAssignTotalCountTask : IAutoCleanAssignTotalCountTask
    {
        private readonly INLogService _logger;
        private readonly IFreeSql _freeSql;
        public AutoCleanAssignTotalCountTask(IFreeSql freeSql, INLogService logger)
        {
            _logger = logger;
            _freeSql = freeSql;
        }

        public async Task CleanAssignTotalCountAsync()
        {
            try
            {
                var res = await _freeSql.Update<SysAdminEntity>().Where(m => m.RoleId == "2").Set(m => m.AssignTotalCount == 0).ExecuteAffrowsAsync();
                if (res >= 0)
                {
                    var str = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}>>>>自动清理业务员的分配订单计数成功";
                    _logger.Info(str);
                    Console.WriteLine(str);
                }
                else
                {
                    var error = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}>>>>自动清理业务员的分配订单计数失败";
                    _logger.Error(error);
                    Console.WriteLine(error);
                }
            }
            catch (Exception ex)
            {
                var error = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}>>>>自动清理业务员的分配订单计数异常>>>>>{ex.Message}";
                _logger.Error(error);
                Console.WriteLine(error);
            }
        }
    }
}


